aws | aws rpc wrapper | Cell 2 | Search

This code provides a function latestS3 that retrieves the 5 most recently modified files from an S3 bucket, optionally filtering them based on a given pattern. It's designed to be used both as a server-side module and potentially in a client-side environment.

Run example

npm run import -- "latest s3 bucket"

latest s3 bucket

var fs = require('fs');
var path = require('path');
var minimatch = require("minimatch")
var AWS = require('aws-sdk');
var importer = require('../Core');
var s3 = new AWS.S3();

// For dev purposes only
var AWS_HTTP = 'https://s3-us-west-2.amazonaws.com/selenium-bots/';
var PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || '';
var key = JSON.parse(fs.readFileSync(path.join(PROFILE_PATH, '.credentials/aws-sdk.json')).toString());
AWS.config.update(key);

function getS3Objects(bucket, marker) {
    return new Promise((resolve, reject) => s3.listObjects({
        Bucket: bucket || 'selenium-bots',
        Prefix: '',
        Marker: marker
    }, function (err, resp) {
        if(err) {
            return reject(err);
        }
        return resolve(resp);
    }))
    .then(resp => {
        if(resp.IsTruncated) {
            return getS3Objects(bucket, resp.Contents[resp.Contents.length-1].Key)
                .then(contents => resp.Contents.concat(contents));
        }
        return resp.Contents;
    })
}

function latestS3(match, bucket) {
    return getS3Objects(bucket)
        .then(files => {
            return files.sort((a, b) => b.LastModified.getTime() - a.LastModified.getTime())
               .filter(s => !match || minimatch(s.Key, match))
               .slice(0, 5)
               .map(i => AWS_HTTP + i.Key)
        });
}
module.exports = latestS3;

if(typeof $ !== 'undefined') {
    $.async();
    latestS3()
        .then(r => $.sendResult(r))
        .catch(e => $.sendError(e))
}

What the code could have been:

const fs = require('fs');
const path = require('path');
const minimatch = require('minimatch');
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
const importer = require('../Core');

// Configuration constants
const AWS_HTTP = 'https://s3-us-west-2.amazonaws.com/selenium-bots/';
const PROFILE_PATH = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE || '';
const CREDENTIALS_FILE = path.join(PROFILE_PATH, '.credentials/aws-sdk.json');

// Load AWS credentials from file
const loadCredentials = () => {
    try {
        const credentials = fs.readFileSync(CREDENTIALS_FILE, 'utf8');
        return JSON.parse(credentials);
    } catch (error) {
        throw new Error('Failed to load AWS credentials');
    }
};

// Update AWS config with credentials
const updateAWSConfig = (credentials) => {
    AWS.config.update(credentials);
};

// Get S3 objects from a bucket
const getS3Objects = (bucket, marker) => {
    return s3.listObjects({
        Bucket: bucket ||'selenium-bots',
        Prefix: '',
        Marker: marker
    }).promise()
       .then((resp) => {
            if (resp.IsTruncated) {
                return getS3Objects(bucket, resp.Contents[resp.Contents.length - 1].Key);
            }
            return resp.Contents;
        });
};

// Get the latest S3 objects that match a pattern
const getLatestS3 = (match, bucket) => {
    return getS3Objects(bucket)
       .then((files) => {
            return files.sort((a, b) => b.LastModified.getTime() - a.LastModified.getTime())
               .filter((s) =>!match || minimatch(s.Key, match))
               .slice(0, 5)
               .map((i) => AWS_HTTP + i.Key);
        });
};

// Export the getLatestS3 function
module.exports = getLatestS3;

// Initialize AWS config and export the function
const init = () => {
    const credentials = loadCredentials();
    updateAWSConfig(credentials);
    return getLatestS3();
};

if (typeof $!== 'undefined') {
    init()
       .then((r) => $.sendResult(r))
       .catch((e) => $.sendError(e));
}

This code defines a function latestS3 that retrieves the 5 most recently modified files from an S3 bucket that match a given pattern.

Here's a breakdown:

  1. Dependencies:

  2. AWS Configuration:

  3. getS3Objects Function:

  4. latestS3 Function:

  5. Module Export:

  6. Client-Side Execution (Conditional):

Let me know if you have any more questions!